<?php
require_once (ADMIN_CONTROLLER_FILE);

class Validation {
    
    private static $errorMSG;
    
    public static function getErrorMSG() {
        return self::$errorMSG;
    }
    
    public static function ValidateUsername($uname) {
        if ( strlen($uname) == 0 ) {
            self::$errorMSG = "Username must be filled out";  
            return false;
        }
        if ( !preg_match('/^[A-Za-z0-9]{3,30}$/', $uname) ) {
            if ( strlen($uname) < 3 ||strlen($uname) > 30) {
                self::$errorMSG = "Username length must be between 3 to 30 characters";  
            }
            else {
                self::$errorMSG = "Username contains illegal characters";  
            }
            return false;
        }
        //Check if user already exists
        require_once (DB_FILE);
        require_once (USER_CONTROLLER_FILE);
        $db = new DBConnection();
        $user_cont = new UserController($db);
        
        // fisrt check that uname != admin uname
        if ( $uname == AdminController::getAdminUser() ) {
            self::$errorMSG = "This username already exists please choose a different one"; 
            return false;
        }        
        
        if ($user_cont->isUserExist($uname)) {
            self::$errorMSG = "This username already exists please choose a different one"; 
            return false;
        }
        else {
            // isUserExist() return FALSE, check if there is an ERROR
            if (strlen($user_cont->getErrorMSG()) > 0 ) {
                if (DEBUG) {
                    self::$errorMSG = "ValidateUsername(): " . $user_cont->getErrorMSG(); 
                }
                else {
                    self::$errorMSG = "Connection Problems, please try again"; 
                }
                return false;                
            }
        }
        // OK!
        return true;
    }
    
    public static function ValidatePassword($pass) {
        if ( strlen($pass) == 0 ) {
            self::$errorMSG = "Password must be filled out";          
            return false;
        }    
        return true;
    }   
    
    public static function ValidateEmail($email) {
        if ( strlen($email) > 100 ) {
            self::$errorMSG = "Email length must be up to 100 characters";          
            return false;
        }    
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            self::$errorMSG= "Not a valid e-mail address";
            return false;            
        }
        return true;
    }       
    
    public static function ValidateNickname($nick) {
        if ( strlen($nick) == 0 ) {
            self::$errorMSG = "Nickname must be filled out";  
            return false;
        }
        if ( !preg_match('/^[A-Za-z0-9 ]{3,20}$/', $nick) ) {
            if ( strlen($nick) < 3 ||strlen($nick) > 20) {
                self::$errorMSG = "Nickname length must be between 3 to 20 characters";  
            }
            else {
                self::$errorMSG = "Nickname contains illegal characters";  
            }
            return false;
        }
        //Check if Nick already exists
        require_once (DB_FILE);
        require_once (USER_CONTROLLER_FILE);
        $db = new DBConnection();
        $user_cont = new UserController($db);
        if ($user_cont->isNickNameExist($nick)) {
            self::$errorMSG = "This nickname already exists please choose a different one"; 
            return false;
        }
        else {
            // isUserExist() return FALSE, check if there is an ERROR
            if (strlen($user_cont->getErrorMSG()) > 0 ) {
                if (DEBUG) {
                    self::$errorMSG = "ValidateNickname(): " . $user_cont->getErrorMSG(); 
                }
                else {
                    self::$errorMSG = "Connection Problems, please try again"; 
                }
                return false;                
            }
        }        
        return true;
    }     
    
    public static function ValidateRegisteration($uname, $pass, $email, $nick) {
        
        if ( self::ValidateUsername($uname) &&
             self::ValidatePassword($pass) && 
             self::ValidateEmail($email) && 
             self::ValidateNickname($nick) ) {
            return true;
        }
        return false;
    }
}